<?
error_reporting(E_ERROR | E_PARSE);

require_once('config.php');
define('DEFAULT_MODULE','underconstruction');
define('DEFAULT_ACTION','index');
require_once(LIB.'/class_Template.php');
require_once(LIB.'/class_User.php');
require_once(LIB.'/class_Group.php');
require_once(LIB.'/class_Modules.php');
require_once(LIB.'/common_functions.php');
require_once(WEBSITE_ROOT.'/common/init.php');
require_once(MODULE_ROOT.'/modules.php');

global $_modules;


$user_name=SESSION_value('user_name','');
$user_id=SESSION_value('user_id',0);
$module=GET_value('m',DEFAULT_MODULE);
$action=GET_value('a',DEFAULT_ACTION);

if ($user_id){
    $r = User :: get_user_data($user_id);
    if($r['success']){
        $u=$r['data'];
        $group_id = $u['group_id'];    
    }else{
        $group_id = 0;    
    }
}else{
    $group_id = 0;
}

//Call a special module to handle error
require_once MODULE_ROOT."/error/error_controller.php"; // Module error
$error_controller = new error_controller();

//Check if module controller file exist
$controller_file=MODULE_ROOT."/$module/".$module."_controller.php";
if (!file_exists($controller_file)){
    $error_controller -> set_error("Module <strong>$module</strong> not found!!");
    $error_controller -> fatality();
}else{
    require_once $controller_file;
    $controller_class = $module."_controller";
    $controller = new $controller_class();    
    $controller_action = "exec_".$action;
    if (method_exists($controller,$controller_action)){
        //Should implement checking authority here!
        $access = Group :: check_permission($group_id, $module, $action);
        if ($access || 'root'==$user_name){
            $view_data = $controller -> $controller_action();
        }else{
            $error_controller -> set_error("You have no permission to access!");
            $error_controller -> fatality();
        }
    }
    else
    {
        $error_controller -> set_error("Action $action not found!");
        $error_controller -> fatality();
    }
}    

if ($error_controller->is_fatality()){
    //echo 'Hello';
    var_dump($error_controller);
    $view_data = $error_controller -> exec_error();
}
//No template
